import 'package:myturngame/bean/bagobject/bag_object.dart';
import 'package:myturngame/bean/bagobject/suit.dart';
import 'package:myturngame/bean/function/normal_equip_function.dart';
import 'package:myturngame/bean/function/skill001_equip_function.dart';
import 'package:myturngame/bean/player.dart';
import 'package:myturngame/bean/skill/skill.dart';

import '../function/abstract_function.dart';

///装备对象
class Equipment extends BagObject {
  ///装备的位置类型
  static String typeHat = "头盔";
  static String typeWeapon = "武器";
  static String typeClothe = "衣服";
  static String typeShoe = "鞋子";

  String position;

  bool isEquip = false;

  //套装属性
  Suit? suit;

  Equipment(super.name, this.position, super.description,
      {super.objectFunctions});

  ///装备
  void equip(Player player) {
    Equipment? bodyEquipment;
    if (position == typeHat) {
      bodyEquipment = player.equipHead;
      player.equipHead = this;
    } else if (position == typeWeapon) {
      bodyEquipment = player.equipWeapon;
      player.equipWeapon = this;
    } else if (position == typeClothe) {
      bodyEquipment = player.equipBody;
      player.equipBody = this;
    } else if (position == typeShoe) {
      bodyEquipment = player.equipShoe;
      player.equipShoe = this;
    }
    isEquip = true;
    player.bag.takeOut(this);
    if (bodyEquipment != null) {
      bodyEquipment.isEquip = false;
      player.bag.putIn(bodyEquipment);
    }
    //套装激活状态刷新
    bodyEquipment?.suit?.notInvoke();
    player.bodySuitInvokeStateUpdate();
    //装备属性结算
    _equipChangeAttribute(player);
    player.attributeSettlement();
  }

  ///卸下装备
  void disEquip(Player player) {
    if (position == typeHat) {
      player.equipHead = null;
    } else if (position == typeWeapon) {
      player.equipWeapon = null;
    } else if (position == typeClothe) {
      player.equipBody = null;
    } else if (position == typeShoe) {
      player.equipShoe = null;
    }
    isEquip = false;
    player.bag.putIn(this);
    //套装激活状态刷新
    suit?.notInvoke();
    player.bodySuitInvokeStateUpdate();
    //装备属性结算
    _equipChangeAttribute(player);
    player.attributeSettlement();
  }

  void _equipChangeAttribute(Player player) {
    for (var value in objectFunctions) {
      if (value is NormalEquipFunction) {
        //常规装备属性
        var changeValue = value.value;
        if (!isEquip) {
          changeValue = -changeValue;
        }
        player.equipmentAttrList[value.type] += changeValue;
      } else if (value is Skill001EquipFunction) {
        _skill001FunctionInvoke(value, player);
      } else if (value is Skill002EquipFunction) {
        _skill002FunctionInvoke(value, player);
      }
    }
  }

  ///技能001装备属性激活
  void _skill001FunctionInvoke(Skill001EquipFunction value, Player player) {
    for (var skill in player.skills) {
      if (skill is Skill001) {
        if (!isEquip) {
          skill.damageSum -= value.addAttackCount * 10;
          skill.damageCount -= value.addAttackCount;
        } else {
          skill.damageSum += value.addAttackCount * 10;
          skill.damageCount += value.addAttackCount;
        }
        skill.updateDamageRate();
      }
    }
  }

  ///技能002装备属性激活
  void _skill002FunctionInvoke(Skill002EquipFunction value, Player player) {
    for (var skill in player.skills) {
      if (skill is Skill002) {
        if (!isEquip) {
          skill.criticalRate -= value.criticalRate;
        } else {
          skill.criticalRate += value.criticalRate;
        }
      }
    }
  }
}
